package com.esms.struts.service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.esms.bean.Employee;
import com.esms.bean.EmployeeDAO;
import com.esms.bean.Person;
import com.esms.bean.PersonDAO;
import com.esms.bean.Salaryserviceemployee;
import com.esms.bean.SalaryserviceemployeeDAO;

public class EmployeeService {
	private static final Log log = LogFactory.getLog(EmployeeService.class);
	private EmployeeService() {}
	public static EmployeeService $ = new EmployeeService(); 
	
	public void createNewEmployee(Employee employee) {
		EmployeeDAO.$.insert(employee);
	}
	
	public boolean existSalaryService(int employee) {
		log.info( "ffemployee Id => " +employee );
		List list = SalaryserviceemployeeDAO.$.findByFfemployeeId(employee);
		log.info( "List Size => " + list.size() );
		return list.isEmpty() ? false : true; 
	}
	
	public Salaryserviceemployee[] getSalaryService(int employee) {
		List list = SalaryserviceemployeeDAO.$.findByFfemployeeId(employee);
		Collections.sort(list, new Comparator(){
			public int compare(Object arg0, Object arg1) {
				Salaryserviceemployee salaryserviceemployee1 = (Salaryserviceemployee) arg0;
				Salaryserviceemployee salaryserviceemployee2 = (Salaryserviceemployee) arg0;
				if(salaryserviceemployee1.getSalaryServiceStartDate() == null)
						return -1;
				if(salaryserviceemployee2.getSalaryServiceStartDate() == null)
					return 1;
				return salaryserviceemployee1.getSalaryServiceStartDate().compareTo(salaryserviceemployee2.getSalaryServiceStartDate());
			}
		});
		
		return (Salaryserviceemployee[]) list.toArray(new Salaryserviceemployee[list.size()]);
	}
	 
	public boolean existSpecialService(int employee) {
		//SpecialserviceemployeeDAO
		SalaryserviceemployeeDAO.$.findByFfemployeeId(employee);
		return false;
	}
	
	public Employee getEmployee(int employeeId) {
		return EmployeeDAO.$.findById(employeeId);
	}
	public List getAllEmployee() {
		return EmployeeDAO.$.findAll();
	}
	
//	public List getAllEmployeeByEnterpriseId(String ent) {
//		return EmployeeDAO.$.findAllEmployeeByEnterprise(ent);
//	}
}
